package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.dao.NewsDao;
import news.dao.TopicDao;
import news.entity.News;
import news.entity.Page;
import news.entity.Topic;

public class IndexServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		NewsDao nd=new NewsDao();
		List<News> ce1=nd.showNews(1, 1, 5);
		List<News> ce2=nd.showNews(2, 1, 5);
		List<News> ce3=nd.showNews(5, 1, 5);
		List<Topic> biao=new TopicDao().showTopic();
		
		request.setAttribute("cebian1", ce1);
		request.setAttribute("cebian2", ce2);
		request.setAttribute("cebian3", ce3);
		request.setAttribute("biaoti", biao);
		
		Integer ntid = null;
		int pageNo = 1;
		try {
			ntid = Integer.parseInt(request.getParameter("ntid"));
			System.out.println(ntid);
		} catch (Exception e) {
		}

		try {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		} catch (Exception e) {
		} 
		
		TopicDao to = new TopicDao();
		List<Topic> list = to.showTopic();
		NewsDao nw = new NewsDao();
		Page<News> p = nw.find(pageNo);
		request.setAttribute("xinwen",p );
		request.setAttribute("ntid", ntid);
		request.getSession().setAttribute("top", list);
	
	
		
		request.getRequestDispatcher("index.jsp").forward(request, response);
		
		
		
		
			
	}

}
